package jt.classic.system.action;

import java.sql.Connection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jt.classic.system.ISystem;
import jt.classic.system.context.ISystemContext;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.listener.IListener;
import jt.classic.system.listener.ListenerCenter;
import jt.classic.system.log.LogManager;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserManager;
import jt.classic.system.utils.ResultJsonLoader;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.limp.basework.Column;
import org.limp.basework.SimpleBean4Table;
import org.limp.basework.SimpleBeanMachining;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;
import org.limp.mine.DataTrimmerI;
import org.limp.mine.HTMLTool;
import org.limp.mine.StringTool;

/**
 * 通用主表子表删除操作控制器
 * <p>
 * delete primary table and foreign table with primary key
 * </p>
 * 
 * @author mido
 * 
 */
public class BaseWorkDeleteIIAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Connection conn = null;

		ActionForward forward = null;

		CommonBean4HSQ cb = new CommonBean4HSQ(request);
		Map param = cb.getResource();

		String pkId = StringTool.checkString(param.get("id"));
		String columnName = StringTool.checkString(param.get("columnName"));
		String tableName = StringTool.checkString(param.get("tableName"));
		String listenerID = StringTool.checkString(param.get("listener"));

		String fwname = StringTool.checkString(param.get("forward"));

		if (!columnName.equals("") && !tableName.equals("")) {
			boolean doBasework = true;

			SimpleBeanMachining sbm = null;

			if (!listenerID.equals("")) {
				ListenerCenter lc = ListenerCenter.getInstance();
				IListener lis = lc.getListener(listenerID);

				if (lis == null) {
					ResultJsonLoader.loadErrorJson(request, "listener("
							+ listenerID + ") is null.");

					ISystem.catchErrorMessage(request, "listener[" + listenerID
							+ "] is null.");

					doBasework = false;
				} else {
					try {
						sbm = lis.getSBM();
					} catch (Exception ex) {
						ResultJsonLoader
								.loadErrorJson(request, ex.getMessage());

						ISystem.catchException(request, this, ex);

						doBasework = false;
					}
				}
			}

			if (doBasework) {
				int outro = -3001;

				try {
					SystemDataSource ds = SystemDataSource.getInstance();
					conn = ds.getConnection();

					LogManager lm = new LogManager(conn);

					IUser user = ISystemContext.getSessionUser(request);

					if (user == null) {
						user = UserManager.createGuestUser();
					}

					SimpleBean4Table sb = new SimpleBean4TableImpl(tableName);

					if (sbm != null) {
						sbm.machining(sb, sb.getTable(), cb, conn, -3);
					}

					Column column = sb.getTable().column(columnName);

					String sql = "DELETE FROM " + sb.getTable().getName() + " WHERE "
							+ column.getName() + " = ?";

					DataTrimmerI trimmer = new DataTrimmerI(conn);
					DataTrimmerI.DataItemList dil = trimmer.createDIL();
					dil.set(pkId);
					System.out.println(pkId);
					trimmer.execute(sql, dil);

					if (sbm != null) {
						sbm.machining(sb, sb.getTable(), cb, conn, 2);
					}

					lm.log(user, request.getRemoteAddr(), "", "", tableName,
							"delete", sb.getResource().toString());

					conn.commit();

					ResultJsonLoader.loadSuccessJson(request, null);

					outro = 3001;
				} catch (Exception ex) {
					ResultJsonLoader.loadErrorJson(request, ex.getMessage());
					conn.rollback();
					ISystem.catchException(request, this, ex);
				} finally {
					SystemDataSource.closeConnection(conn);
				}

				if (sbm != null) {
					try {
						/* outro */
						sbm.machining(null, null, cb, null, outro);
					} catch (Exception ex) {
						ResultJsonLoader
								.loadErrorJson(request, ex.getMessage());

						ISystem.catchException(request, this, ex);
					}
				}
			}
		}

		if (fwname.equals("")) {
			forward = mapping.findForward("jsonresult");
		} else {
			forward = new ActionForward();
			forward.setPath(fwname);
			forward.setRedirect(true);
		}

		return forward;
	}
}